<template>
  <!-- 质量验收计划选择 -->
  <a-select v-bind="$attrs" v-on="$listeners" :placeholder="placeholder" v-model="currentValue" allowClear>
    <a-icon v-if="fetching" style="color: #096dd9" slot="suffixIcon" type="loading" />
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-select-option v-for="i in list" :key="i.id" :value="i.id">{{ i.name }}</a-select-option>
  </a-select>
</template>
<script>
import { acceptancePlan } from '@/api/quality/quality' // api

export default {
  name: 'SelectQualityReceivance',
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Object, Number],
    },
    // placeholder
    placeholder: {
      type: String,
      default: function () {
        return '请选择'
      },
    },
    // 联动表单值改变时是否清空当前表单值
    isClear: {
      type: Boolean,
      default() {
        return true
      },
    },
    //如果是编辑，第一次不要重置绑定的值。。。。
    isEdit: {
      type: Boolean,
      default() {
        return false
      },
    },
    projectId: {
      type: String,
      default: function () {
        return ''
      },
    },
    checkAcceptanceTypeId: {
      type: String,
      default: function () {
        return ''
      },
    },
    // 初始化
    isInit: {
      type: Boolean,
      default() {
        return true
      },
    }
  },
  mounted() {
    
  },
  data() {
    return {
      isCanClear: !this.isEdit,
      fetching: false,
      list: [],
    }
  },
  methods: {
    getProjectList() {
      this.$nextTick(() => {
        this.fetching = true
        let params = {
          projectId: this.projectId,
          checkAcceptanceTypeId: this.checkAcceptanceTypeId,
        }
        acceptancePlan
          .planItem(params)
          .then((res) => {
            this.fetching = false
            this.list = res.data
          })
          .catch((res) => {
            this.fetching = false
          })
      })
    },
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        const target = this.$clone(this.list.find((i) => (i.id === this.value)) || {})
        this.$emit('update:selectOption', target)
        this.$emit('updateSelect', target)
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      },
    },
  },
  watch: {
    fetching(e){
      this.$emit('loading',e)
    },
    projectId: {
      handler(val) {
        if (val && this.checkAcceptanceTypeId && this.projectId) {
          // 清空表单
          if (this.isClear) {
            this.currentValue = ''
            this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
          }
          // 清空表单
          if (this.isEdit && this.isCanClear) {
            this.currentValue = ''
            this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
          } else {
            this.isCanClear = true
          }
          // 重置列表
          this.list = []
          val && this.getProjectList()
        }
      },
      immediate: true,
      deep: true
    },
    checkAcceptanceTypeId: {
      handler(val) {
        if (val && this.checkAcceptanceTypeId && this.projectId) {
          // 清空表单
          if (this.isClear) {
            this.currentValue = ''
            this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
          }
          // 清空表单
          if (this.isEdit && this.isCanClear) {
            this.currentValue = ''
            this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
          } else {
            this.isCanClear = true
          }
          // 重置列表
          this.list = []
          val && this.getProjectList()
        }
      },
      immediate: true,
      deep: true
    },
  },
}
</script>
